//
//  JKThemeEditorsViewController.swift
//  ZhiHuDaily-Swift
//
//  Created by albert on 2018/3/21.
//  Copyright © 2018年 安永博. All rights reserved.
//

import UIKit

class JKThemeEditorsViewController: JKBaseViewController {
    
    /** 主编数组 */
    public var editors = [JKThemeEditorModel]()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    override func setupNav() {
        
        navTitle = "主编"
    }
    
    override func setupTableView() {
        
        tableViewPlain.rowHeight = 44
        tableViewPlain.contentInset = UIEdgeInsetsMake(JKNavBarHeight, 0, JKCurrentHomeIndicatorHeight, 0)
        
        if #available(iOS 11.0, *) {
            
        } else {
            
            tableViewPlain.scrollIndicatorInsets = tableViewPlain.contentInset
        }
        
        tableViewPlain.register(UINib(nibName: "JKThemeEditorsCell", bundle: nil), forCellReuseIdentifier: "JKThemeEditorsCell")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

// MARK: - taleview数据源

extension JKThemeEditorsViewController {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return editors.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        var cell = tableView.dequeueReusableCell(withIdentifier: "JKThemeEditorsCell") as? JKThemeEditorsCell
        
        if cell == nil {
            
            cell = JKThemeEditorsCell(style: UITableViewCellStyle.default, reuseIdentifier: "JKThemeEditorsCell")
        }
        
        cell?.editorModel = editors[indexPath.row]
        
        return cell!
    }
}


// MARK: - taleview代理

extension JKThemeEditorsViewController {
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        super.tableView(tableView, didSelectRowAt: indexPath)
        
        let model = editors[indexPath.row];
        
        let urlStr = (model.url == nil) ? "https://zhuanlan.zhihu.com/p/19871622" : model.url!;
        
        guard let url = URL(string: urlStr) else { return }
        
        let request = URLRequest(url: url)
        
        let vc = JKBrowserViewController()
        vc.request = request
        navigationController?.pushViewController(vc, animated: true)
    }
}
